package com.qf.boot2302.config;

import com.qf.boot2302.filter.CheckPasswordFilter;
import com.qf.boot2302.filter.CheckUserNameFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 如何通过配置类在springboot项目中注册filter
 */
@Configuration
public class FilterConfig {


    @Bean
    public FilterRegistrationBean<CheckUserNameFilter> checkUserNameFilterFilterRegistrationBean(){

        FilterRegistrationBean<CheckUserNameFilter> registrationBean = new FilterRegistrationBean<>();

        //1. 设置filter对象
        registrationBean.setFilter(new CheckUserNameFilter());

        //2. 设置filter要处理的url路径
        registrationBean.addUrlPatterns("/hi","/login/dologin");

        //3. 指定filter的顺序
        registrationBean.setOrder(Integer.MIN_VALUE);

        return registrationBean;

    }


    @Bean
    public FilterRegistrationBean<CheckPasswordFilter> checkPasswordFilterFilterRegistrationBean(){

        FilterRegistrationBean<CheckPasswordFilter> registrationBean = new FilterRegistrationBean<>();
        // 要注册的filter对象
        registrationBean.setFilter(new CheckPasswordFilter());

        // 处理filter的url路径
        registrationBean.addUrlPatterns("/hi","/login/dologin");

        //3. 指定filter的顺序
        registrationBean.setOrder(1);

        return registrationBean;
    }

}
